package com.haozi.config;

import cn.hutool.core.util.StrUtil;
import com.haozi.exception.SystemException;
import com.haozi.exception.UseException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zyh
 * @version 1.0
 * @date 2022/4/28 11:51 上午
 */
@Component
public class GlobalArgs {


    private Map<String, String> mapParam;

    boolean isInit = false;

    /**
     * 初始化
     * @param args
     */
    public void init(String[] args){
        if(isInit == false){
            mapParam = new HashMap<>();
            //-config=/test/123
            for (String arg : args) {
                String[] split = arg.split("=");
                if (split.length < 2) {
                    throw new UseException("启动参数错误");
                }
                if (!StrUtil.startWith(arg, "-")) {
                    throw new UseException("启动参数错误");
                }
                String key = StrUtil.replace(split[0],"-","");
                mapParam.put(key,split[1]);
            }

        }

    }

    public String getPropertie(String key) {

        if (mapParam != null) {
            return mapParam.get(key);
        }
        mapParam = new HashMap<>();
        return mapParam.get(key);
    }

}
